package cn.kailangtu.commontools;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.Image;
import android.util.Log;

public class TemperatureImageUtils {
    static long count = 0;
    static long woCount = 0;
    public static int temperatureToRGB(int temperature) {
        return temperatureToColor(temperature);


    }

    public static int temperatureToColor(int  temperature) {
        int[] rgb = new int[3];
        if (temperature < 0) {
            rgb[1] = 255 - (temperature * -1);
            rgb[2] = 255;
        } else if (temperature < 50) {
            // 0-50度的温度映射到青色到黄色渐变
            rgb[0] = (int)(155 + (temperature * 0.33));
            rgb[1] = (int)(255 - (temperature * 0.5));

        } else {
            rgb[0] = 255;
            rgb[1] = (int)(255 - ((temperature - 50) * 1.5));
            // 高于50度的温度映射到红色

        }
        return    convertRGBtoInt(rgb[0],rgb[1],rgb[2]);
    }


    public static int convertRGBtoInt(int red, int green, int blue) {
        return (red << 16) | (green << 8) | blue;
    }


    private static Bitmap bitmap1,bitmap2,bitmap3;
    private static Paint paint1,paint2,paint3;
    private static Canvas canvas1,canvas2,canvas3;

    private static Canvas BuildCanvas1()
    {
        if(paint1 == null) {
            paint1 = new Paint();
            paint1.setAntiAlias(true);
            paint1.setColor(0xFFFF0000); // 红色点
            // 创建点阵位图
            bitmap1 = Bitmap.createBitmap(240, 320, Bitmap.Config.ARGB_8888);
            canvas1 = new Canvas(bitmap1);
        }
        return canvas1;
    }

    private static Canvas BuildCanvas2()
    {
        if(paint2 == null) {
            paint2 = new Paint();
            paint2.setAntiAlias(true);
            paint2.setColor(0xFFFF0000); // 红色点
            // 创建点阵位图
            bitmap2 = Bitmap.createBitmap(240, 320, Bitmap.Config.ARGB_8888);
            canvas2 = new Canvas(bitmap2);
        }
        return canvas2;
    }

    private static Canvas BuildCanvas3()
    {
        if(paint3 == null) {
            paint3 = new Paint();
            paint3.setAntiAlias(true);
            paint3.setColor(0xFFFF0000); // 红色点
            // 创建点阵位图
            bitmap3 = Bitmap.createBitmap(240, 320, Bitmap.Config.ARGB_8888);
            canvas3 = new Canvas(bitmap3);
        }
        return canvas3;
    }


    public static Bitmap buildImage90ForIndex(int[] source,int mapIndex){


        int index = 0;
        int[][] bitmap = new int[32][24];
        for(int r = 0;r< 24;r++){
            for(int c = 0;c < 32;c++)
            {
                bitmap[c][r] = temperatureToRGB(source[index]);
                index++;
            }
        }

        if(mapIndex  == 0)
        {
            Canvas canvas = BuildCanvas1();
            return  process(canvas,paint1,bitmap,bitmap1);
        }
        else if(mapIndex == 1){
            Canvas canvas = BuildCanvas2();
            return  process(canvas,paint2,bitmap,bitmap2);
        }
        else {
            Canvas canvas = BuildCanvas3();
            return  process(canvas,paint3,bitmap,bitmap3);
        }
    }

    private static Bitmap process(Canvas canvas,Paint paint,int[][] bitmap,Bitmap result)
    {
        for(int x = 0;x < 32;x++){
            for(int y = 0;y < 24; y++)
            {

                int rectX = y * 10;
                int rectY = (32-x) * 10;


                Rect rect = new Rect(rectX,rectY,rectX+10,rectY+10);
                paint.setColor(bitmap[x][y]); // 红色点
                canvas.drawRect(rect,paint);

                count++;
            }
        }

        return result;
    }


    public static Bitmap buildImage90(int[] source){

        if(woCount < (24*32))
        {
            //Loggers.testInfo("数据长度："+source.length);
        }
        int index = 0;
        int[][] bitmap = new int[32][24];
        for(int r = 0;r< 24;r++){
            for(int c = 0;c < 32;c++)
            {

                bitmap[c][r] = temperatureToRGB(source[index]);
                index++;

                woCount++;
            }
        }

        Bitmap dotBitmap = null;
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(0xFFFF0000); // 红色点

        // 创建点阵位图
        dotBitmap = Bitmap.createBitmap(240, 320, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(dotBitmap);
        for(int x = 0;x < 32;x++){
            for(int y = 0;y < 24; y++)
            {

                int rectX = y * 10;
                int rectY = (32-x) * 10;


                Rect rect = new Rect(rectX,rectY,rectX+10,rectY+10);
                paint.setColor(bitmap[x][y]); // 红色点
                canvas.drawRect(rect,paint);

                count++;
            }
        }

        return dotBitmap;

    }


    public static Bitmap buildImage(int[] source){

        if(woCount < (24*32))
        {
           // Loggers.testInfo("数据长度："+source.length);
        }
        int index = 0;
        int[][] bitmap = new int[32][24];
        for(int r = 0;r< 24;r++){
            for(int c = 0;c < 32;c++)
            {

                bitmap[c][r] = temperatureToRGB(source[index]);
                index++;

                woCount++;
            }
        }

        Bitmap dotBitmap = null;
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(0xFFFF0000); // 红色点

        // 创建点阵位图
        dotBitmap = Bitmap.createBitmap(320, 240, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(dotBitmap);
        for(int x = 0;x < 32;x++){
            for(int y = 0;y < 24; y++)
            {

                Rect rect = new Rect(10* x,10 * y,(10 * x)+10,(10 * y)+10);
                paint.setColor(bitmap[x][y]); // 红色点
                canvas.drawRect(rect,paint);

                count++;
            }
        }

        return dotBitmap;

    }

}
